package mspbots.data.tl.base;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.log4j.Log4j2;
import mspbots.data.tl.entity.BaseEntity;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.r2dbc.core.DatabaseClient;
import org.springframework.util.ObjectUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

/**
 * @author billb
 */
@Log4j2
public abstract class AbstractBaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> extends ServiceImpl<M, T> {

    protected DatabaseClient databaseClient;
    protected ObjectMapper objectMapper;
    protected RabbitMessagingTemplate messagingTemplate;

    @Autowired
    public void setObjectMapper(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Autowired
    public void setMessagingTemplate(RabbitMessagingTemplate messagingTemplate) {
        this.messagingTemplate = messagingTemplate;
    }

    @Autowired
    public void setDatabaseClient(DatabaseClient databaseClient) {
        this.databaseClient = databaseClient;
    }

    protected Long getLongValue(JsonNode node){
        try {
            return ObjectUtils.isEmpty(node) ? null : node.asLong();
        } catch (Exception e) {
            log.error("(Please Ignore--Data Error)getLongValue Error" + e.getMessage(),node);
            return null;
        }
    }

    protected String getStringValue(JsonNode node){
        try {
            return ObjectUtils.isEmpty(node) || "null".equalsIgnoreCase(node.asText()) ? null : node.asText();
        } catch (Exception e) {
            log.error("(Please Ignore--Data Error)getStringValue Error" + e.getMessage(),node);
            return null;
        }
    }

    protected LocalDateTime getDateTimeValue(JsonNode node){
        try {
            return ObjectUtils.isEmpty(node) || "null".equalsIgnoreCase(node.asText()) ? null : LocalDateTime.parse(node.asText(), DateTimeFormatter.ISO_DATE_TIME);
        }catch (DateTimeParseException ee){
            return ObjectUtils.isEmpty(node) || "null".equalsIgnoreCase(node.asText()) ? null : LocalDateTime.parse(node.asText(), DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        }catch (Exception e) {
            log.error("(Please Ignore--Data Error)getDateTimeValue Error" + e.getMessage(),node);
            return null;
        }
    }

    protected LocalDate getDateValue(JsonNode node){
        try {
            return ObjectUtils.isEmpty(node) || "null".equalsIgnoreCase(node.asText()) ? null : LocalDate.parse(node.asText(), DateTimeFormatter.ISO_DATE);
        } catch (Exception e) {
            log.error("(Please Ignore--Data Error)getDateTimeValue Error" + e.getMessage(),node);
            return null;
        }
    }

    protected Double getDoubleValue(JsonNode node){
        try {
            return ObjectUtils.isEmpty(node) ? null : node.asDouble();
        } catch (Exception e) {
            log.error("(Please Ignore--Data Error)getDoubleValue Error" + e.getMessage(),node);
            return null;
        }
    }

    protected Integer getIntegerValue(JsonNode node){
        try {
            return ObjectUtils.isEmpty(node) ? null : node.asInt();
        } catch (Exception e) {
            log.error("(Please Ignore--Data Error)getIntegerValue Error" + e.getMessage(),node);
            return null;
        }
    }

    protected Boolean getBooleanValue(JsonNode node){
        try {
            return ObjectUtils.isEmpty(node) ? null : node.asBoolean();
        } catch (Exception e) {
            log.error("(Please Ignore--Data Error)getBooleanValue Error" + e.getMessage(),node);
            return null;
        }
    }

}
